//Documentation complete
/**
 * @author Alex Peterson
 * @version 2009MR30
 */

package broker;

import problemDomain.ErrorBean;
import static problemDomain.ErrorBean.ErrorCode.*;
import static problemDomain.ErrorBean.*;
import utilities.MySQLConnection;

import java.sql.*;

/**
 * An interface that defines a database broker.
 */
public abstract class ReportBroker {
	
	/* Attributes */
	
	protected Connection conn;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	protected ReportBroker () throws ErrorBean {
		open();
	}
	
	/* END Constructors */
	
	
	/* Methods */
	
	protected final PreparedStatement prepare (PreparedStatement statement, String sql) throws ErrorBean {
		if (!isConnected())
			throwError(
				BROKER_CONNECTION_NOT_AVAILABLE,
				"Not connected.",
				this);
		
		try {
			if (statement == null)
				statement = conn.prepareStatement(sql);
			return statement;
		} catch (SQLException e) {
			throwError(
				BROKER_PREPARED_STATEMENTS_NOT_AVAILABLE,
				e.getMessage(),
				this);
			return null;
		}
	}
	
	protected final CallableStatement prepare (CallableStatement call, String sql) throws ErrorBean {
		if (!isConnected())
			throwError(
				BROKER_CONNECTION_NOT_AVAILABLE,
				"Not connected.",
				this);
		
		try {
			if (call == null)
				call = conn.prepareCall(sql);
			return call;
		} catch (SQLException e) {
			throwError(
				BROKER_PREPARED_STATEMENTS_NOT_AVAILABLE,
				e.getMessage(),
				this);
			return null;
		}
	}
	
	protected final CallableStatement registerOutParameter (CallableStatement statement, int parameterIndex, int sqlType) throws ErrorBean {
		try {
			statement.registerOutParameter(parameterIndex, sqlType);
			return statement;
		} catch (SQLException e) {
			throwError(
				BROKER_PREPARED_STATEMENTS_NOT_AVAILABLE,
				e.getMessage(),
				this);
			return null;
		}
	}
	/**
	 * Sets up an SQL clause to be appended to an SQL statement.
	 * @param clause sql clause that is to be appended to an sql statement
	 * @param isFirst boolean determining if the current clause is the first to be appended
	 * @return a string to be appended to an sql statement
	 */
	protected final String appendClause (String clause, boolean isFirst) {
		String conj;
		if (isFirst)
			conj = " WHERE ";
		else 
			conj = " AND ";
		return conj + clause;
	}
	
	/**
	 * Opens the connection to the database.
	 * @throws ErrorBean
	 */
	protected final void open () throws ErrorBean {
		if (!isConnected())
			conn = MySQLConnection.createConnection();
	}
	
	/**
	 * Closes the connection to the database.
	 * @throws ErrorBean
	 */
	public final void close () throws ErrorBean {
		try {
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			throwError(
				BROKER_UNABLE_TO_CLOSE_CONNECTION,
				e.getMessage(),
				this);
		}
	}
	
	/**
	 * Returns whether the <code>Broker</code> is connected.
	 * @return <code>true</code> if the <code>Broker</code> is connected, <code>false</code> otherwise.
	 */
	public final boolean isConnected () {
		try {
			return conn != null && !conn.isClosed();
		} catch (SQLException e) {
			return false;
		} 
	}
	
	/* END Methods */
	
}
